import java.util.ArrayList;
import java.util.List;

/**
 * 给定一个left和一个right,left<right
 * 输出[left,right]这个范围内所有的数
 * 输出的数能被自身各个位上的数整除
 * 栗子:128(128% 1 == 0, 128 % 2 == 0, 128 % 8 == 0)
 */
public class SelfDividingNumbers {

    private List<Integer> selfDividingNumbers(int left, int right) {
        int len = right - left + 1;
        List<Integer> list = new ArrayList<>();
        for (int i = left; i <= len; i++) {
            int no = (i + "").length();
            int a;
            int b = i;
            int num = 0;
            for (int j = 0; j < no; j++) {
                a = b % 10;
                b = b / 10;
                if (a != 0) num = i % a == 0 ? num + 1 : num;
            }
            if (num == no)
                list.add(i);
        }
        return list;
    }

    public static void main(String[] args) {
        int left = 1,right = 22;
        List<Integer> list = new SelfDividingNumbers().selfDividingNumbers(left, right);
        StringBuilder sb = new StringBuilder("");
        System.out.print("[");
        for (Integer integer : list) {
            sb.append(integer).append(", ");
        }
        System.out.print(sb.substring(0, sb.length() - 2) + "]");

    }
}
